package com.dai.chun.dao.action;

import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.dai.chun.dao.dao.EmployeeDao;
import com.dai.chun.dao.dao.PerformanceDao;
import com.dai.chun.dao.dao.SubsidyDao;
import com.dai.chun.dao.dao.WageDao;
import com.dai.chun.dao.dao.WageDaoImpl;
import com.dai.chun.dao.impl.DepartmentDaoImpl;
import com.dai.chun.dao.impl.EmployeeDaoImpl;
import com.dai.chun.dao.impl.PerformanceDaoImpl;
import com.dai.chun.dao.impl.SubsidyDaoImpl;
import com.dai.chun.dao.model.Department;
import com.dai.chun.dao.model.Employee;
import com.dai.chun.dao.model.Performance;
import com.dai.chun.dao.model.Subsidy;
import com.dai.chun.dao.model.Wage;
import com.dai.chun.util.PageSplitUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.logging.Logger;
import com.sun.org.apache.bcel.internal.generic.NEW;

public class WageAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 
	 */
	private WageDao wageDao;
	private Wage wage;
	private List<Wage> wageList;
	private PageSplitUtil psu;
	private Department department;
	private Employee employee;
	private List<Subsidy> subsidyList;
	private List<Department> deptList;

	// 添加
	public String addWage() throws IOException, SQLException {
		// 计算津贴
		processSubsidy(wage);
		// 计算奖励处罚
		processPerformance(wage);
		// 计算工资
		wageAlgorithm(wage);
		logger.info("wage_real:" + wage.getWage_real());
		// 其他的属性有struts自动设置
		wageDao = new WageDaoImpl();
		wageDao.save(wage);

		return "addWage";
	}
	// 添加
	public String moveWageTable() throws IOException, SQLException {
		wageDao = new WageDaoImpl();
		wageDao.moveWageTable(wage.getWage_month());
		return "succeed";
	}
	
	//生成本月工资
	public String generateWages() throws IOException, SQLException {
		//查出所有员工
		EmployeeDao employeeDao=new EmployeeDaoImpl();
		List<Employee> empList=employeeDao.listAllEmployee();
		List<Wage> wageList=new ArrayList<Wage>();
		for(Employee emp:empList){//为一位员工计算工资
			Wage w=new Wage();
			//设置基本信息
			w.setWage_base(emp.getEmp_wage_base());// 基本工资
			w.setWage_emp_num(emp.getEmp_num());// 员工编号
			w.setWage_dept_id(emp.getEmp_dept_id());// 所在部门
			w.setWage_emp_id(emp.getEmp_id());// 员工ID
			w.setWage_emp_name(emp.getEmp_name());// 员工姓名
			w.setWage_month(new SimpleDateFormat("yyyy-MM").format(new Date()));// 月份
			w.setWage_sxyj(emp.getEmp_sxyj());// 三险一金
			w.setWage_tax(emp.getEmp_tax());// 税费
			// 计算津贴
			//processSubsidy(w);
			// 计算奖励处罚
			processPerformance(w);
			// 计算工资
			wageAlgorithm(w);
			wageList.add(w);
		}
		//全部存入数据库
		wageDao = new WageDaoImpl();
		wageDao.save(wageList);
		
		return "generateWages";
	}
	
	// 计算工资的方法
	private void wageAlgorithm(Wage wage) {
		Float wage_real;
		Float wage_should;
		wage_should=wage.getWage_base() * wage.getWage_work_days()
				/ wage.getWage_should_days() + wage.getWage_subsidy_sum()+wage.getWage_reward();
		wage_real =wage_should-wage.getWage_penalty()-wage.getWage_tax();
		wage.setWage_should(wage_should);
		wage.setWage_real(wage_real);

	}

	// 计算津贴
	private void processSubsidy(Wage wage) {
		// 根据前台选择的适用的津贴：Effect_subsidys 获得适用的津贴的链表
		SubsidyDao subsidyDao = new SubsidyDaoImpl();
		subsidyList = subsidyDao.getCurSubsidyList(wage.getEffect_subsidys());
		// 为津贴详细内容subsidy_detail设值 计算津贴总数
		String subsidy_detail = "<ul>";
		Float subsidy_sum = 0F;
		for (Subsidy s : subsidyList) {
			subsidy_detail += "<li>" + s.getSubsidy_content() + "["
					+ s.getSubsidy_amount() + "]</li>";
			subsidy_sum += s.getSubsidy_amount();
		}
		subsidy_detail += "</ul>";
		wage.setWage_subsidy_detail(subsidy_detail);
		wage.setWage_subsidy_sum(subsidy_sum);
	}
	// 计算奖励处罚
	private void processPerformance(Wage wage) {
		//查找员工的奖励处罚信息
		List<Performance> performanceList;
		PerformanceDao performanceDao=new PerformanceDaoImpl();
		performanceList=performanceDao.listPerformance(wage.getWage_emp_id(),wage.getWage_month());
		for(Performance per:performanceList){
			if(per.getPerformance_treat_type().equals("奖励")){
				wage.setWage_reward(wage.getWage_reward()+per.getPerformance_treat_amount());
			}else if (per.getPerformance_treat_type().equals("处罚")) {
				wage.setWage_penalty(wage.getWage_penalty()+per.getPerformance_treat_amount());
			}
			
		}
	}

	// 显示添加工资的页面
	public String showAddWage() throws IOException, SQLException {
		EmployeeDao employeeDao = new EmployeeDaoImpl();
		employee = employeeDao.findEmployeeById(employee.getEmp_id());

		SubsidyDao subsidyDao = new SubsidyDaoImpl();
		subsidyList = subsidyDao.getCurSubsidyList(employee);

		return "showAddWage";
	}

	// 显示所有工资
	public String listWage() throws IOException, SQLException {
		DepartmentDaoImpl departmentDao = new DepartmentDaoImpl();
		setDeptList(departmentDao.listDepartment());

		wageDao = new WageDaoImpl();
		if (psu == null) {//从action进入页面  默认分页
			psu = new PageSplitUtil(1);
		}
		psu.comput();//计算分页
		psu.computPageCount(wageDao.findAllCount());
		wageList = wageDao.listWage(psu);
		return "listWage";
	}

	// 工资统计
	public String checkWage() throws IOException, SQLException {
		DepartmentDaoImpl departmentDao = new DepartmentDaoImpl();
		setDeptList(departmentDao.listDepartment());

		wageDao = new WageDaoImpl();
		if (psu == null) {
			psu = new PageSplitUtil(1);
		}
		psu.comput();
		psu.computPageCount(wageDao.findAllCount());
		wageList = wageDao.listWage(psu);
		return "checkWage";
	}

	// 显示指定工资 查找
	public String searchWage() {

		String sql = "select * from t_wage where";
		if (!wage.getWage_month().equals("")) {
			//有月份条件限制
			sql += " wage_month=" + wage.getWage_month();

		} else if (0 != wage.getWage_dept_id()) {
			//有部门条件限制
			sql += "  wage_dept_id='" + wage.getWage_dept_id();
		}
		if (sql.endsWith("where")) {
			// 即使提交的条件全是空，也会执行
			sql = sql.substring(0, sql.indexOf("where"));
		}
		sql = sql + ";";// 加分号
		wageDao = new WageDaoImpl();
		wageList = wageDao.searchWage(sql);
		return "searchWage";
	}
	/**
	 *  列出指定员工的工资
	 * @return
	 * @throws IOException
	 * @throws SQLException
	 */
	public String listWageByEmpId() throws IOException, SQLException {
		wageDao = new WageDaoImpl();
		if (psu == null) {
			psu = new PageSplitUtil(1);
		}
		psu.comput();
		psu.computPageCount(wageDao.findAllCount());
		wageList = wageDao.listWageByDeptId(psu, employee.getEmp_id());
		return "listWageByEmpId";
	}

	// 删除工资
	public String delWage() throws SQLException {
		wageDao = new WageDaoImpl();
		wageDao.delWage(wage.getWage_id());
		return "delWage";
	}

	public List<Wage> getWageList() {
		return wageList;
	}

	public void setWageList(List<Wage> wageList) {
		this.wageList = wageList;
	}

	public Wage getWage() {
		return wage;
	}

	public void setWage(Wage wage) {
		this.wage = wage;
	}

	public PageSplitUtil getPsu() {
		return psu;
	}

	public void setPsu(PageSplitUtil psu) {
		this.psu = psu;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public List<Subsidy> getSubsidyList() {
		return subsidyList;
	}

	public void setSubsidyList(List<Subsidy> subsidyList) {
		this.subsidyList = subsidyList;
	}

	public List<Department> getDeptList() {
		return deptList;
	}

	public void setDeptList(List<Department> deptList) {
		this.deptList = deptList;
	}

}